package com.wyl.mine.setting

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.LinearLayoutManager
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.MINE_HELP_LIST
import com.wyl.libbase.base.NormalActivity
import com.wyl.libbase.binding.recyclerview.RecyclerViewDivider
import com.wyl.libbase.extend.openActivity
import com.wyl.libbase.extend.toast
import com.wyl.mine.R
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.SingleTypeAdapter
import kotlinx.android.synthetic.main.help_list_activity.*

class HelpListActivity : NormalActivity(), ItemClickPresenter<Map<String, String>> {

    override fun getLayoutId(): Int = R.layout.help_list_activity

    override fun onItemClick(v: View, item: Map<String, String>) {
        openActivity(HelpDetailActivity::class.java) {
            putExtra("id", item["id"])
        }
    }

    private val dataSource = ObservableArrayList<Map<String, String>>()

    override fun initView() {
        titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }

        titlebar.centerTextView.text = "常见问题"

        recyclerview.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = SingleTypeAdapter(context, R.layout.help_item, dataSource).apply {
                itemPresenter = this@HelpListActivity
                itemDecorator
            }
            addItemDecoration(RecyclerViewDivider(context, RecyclerViewDivider.VERTICAL))
        }
        


    }

    override fun loadData() {
        OkGo.post<BaseResponse<List<Map<String, String>>>>(MINE_HELP_LIST)
            .converter(object : JsonConvert<BaseResponse<List<Map<String, String>>>>() {})
            .adapt(ObservableBody<BaseResponse<List<Map<String, String>>>>())
            .map { it.data }
            .subscribe({
                dataSource.addAll(it)
            }, {
                toast(onError(it))
                it.printStackTrace()
            }).toDisposables(disposables)
    }

}
